package com.qf.sl.youshare.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.qf.sl.youshare.R;
import com.qf.sl.youshare.model.User;
import com.qf.sl.youshare.model.UserLogin;
import com.qf.sl.youshare.tasks.TaskProcessor;
import com.qf.sl.youshare.tasks.UserLoginTask;
import com.qf.sl.youshare.utils.UserCurrent;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by IDEA
 * User : SL
 * on  2015/2/15.
 * 20:51
 */
public class LoginActivity extends Activity implements View.OnClickListener, TaskProcessor {

    private EditText userName;
    private EditText userPassword;
    private Button loginBtn;
    private Button registerBtn;
    private String userNameStr;
    private String userPasswordStr;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);

        userName = (EditText) findViewById(R.id.text_login_user_name);
        userPassword = (EditText) findViewById(R.id.text_login_user_password);
        loginBtn = (Button) findViewById(R.id.btn_login_login);
        registerBtn = (Button) findViewById(R.id.btn_login_register);

        loginBtn.setOnClickListener(this);
        registerBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        int id = v.getId();

        if (id == R.id.btn_login_register){
            Intent intent = new Intent(this, UserRegisterActivity.class);
            startActivity(intent);
        }

        userNameStr = userName.getText().toString();
        userPasswordStr = userPassword.getText().toString();

        if (id == R.id.btn_login_login) {

            if (!userNameStr.equals("") && userNameStr.length() > 0) {
                if (!userPasswordStr.equals("") && userPasswordStr.length() > 0) {

                    //TODO 处理登录请求

                    UserLoginTask task = new UserLoginTask(this);
                    task.execute(userNameStr, userPasswordStr);

//                    String url = Constants.USER_URL +
//                                 "user=" + userNameStr +
//                                 "&password=" + userPasswordStr;
//                    byte[] bytes = HttpTools.get(url);

//                    List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
//                    params.add(new BasicNameValuePair("ac","login"));
//                    params.add(new BasicNameValuePair("user",userNameStr));
//                    params.add(new BasicNameValuePair("password",userPasswordStr));

//                    HashMap<String, String> params = new HashMap<String, String>();
////                    params.put("ac", "login");
//                    params.put("user", userNameStr);
//                    params.put("password", userPasswordStr);
//
//                    String url = Constants.USER_URL + "?ac=login";
//                    byte[] bytes = HttpTools.post(url, params, "UTF-8");
//
//                    try {
//                        String string = new String(bytes, "UTF-8");
//                        JSONObject jsonObject = new JSONObject(string);
//                        int code = jsonObject.getInt("code");
//                        if (code == 1) {
//                            JSONObject msg = jsonObject.getJSONObject("msg");
//
//                            //TODO 记录当前登录用户
//                            UserCurrent userCurrent = UserCurrent.getInstance();
//                            UserLogin userLogin = userCurrent.getUserLogin();
//                            if (userLogin != null) {
//                                userLogin.parseJson(msg);
//                            }
//                            userCurrent.setUserLogin(userLogin);
//
//                            User user = new User();
//                            user.setName(userNameStr);
//                            user.setPassword(userPasswordStr);
//                            userCurrent.setUser(user);
//
//                            finish();
//
//                        } else {
//                            String msg = jsonObject.getString("msg");
//                            Toast.makeText(this, "登录失败:" + msg, Toast.LENGTH_SHORT).show();
//                        }
//                    } catch (UnsupportedEncodingException e) {
//                        e.printStackTrace();
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }


                } else {
                    Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();

        UserCurrent current = UserCurrent.getInstance();
        User user = current.getUser();
        UserLogin userLogin = current.getUserLogin();
        if (userLogin != null && user != null) {
            finish();
        }
    }

    @Override
    public void processResult(JSONObject jsonObject) {
        try {
            int code = jsonObject.getInt("code");
            if (code == 1){
                JSONObject msg = jsonObject.getJSONObject("msg");

                //TODO 记录当前登录用户
                UserCurrent userCurrent = UserCurrent.getInstance();
                UserLogin userLogin = userCurrent.getUserLogin();
                if (userLogin != null) {
                    userLogin.parseJson(msg);
                    String userId = userLogin.getUserId();
                    System.out.println("userId = " + userId);
                }else {
                    userLogin = new UserLogin();
                    userLogin.parseJson(msg);
                    userCurrent.setUserLogin(userLogin);
                }

                User user = new User();
                user.setName(userNameStr);
                user.setPassword(userPasswordStr);
                userCurrent.setUser(user);


                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();

                finish();
            }else {
                String msg = jsonObject.getString("msg");
                Toast.makeText(this, "登录失败:" + msg, Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}